﻿using System;
using System.Windows.Forms;

namespace TestesFixacao.WindowsApp.Refatorado.Controls.Shared
{
    public partial class BaseControl : UserControl
    {
        protected object _dataSource;

        public BaseControl()
        {
            InitializeComponent();
        }

        private void ConfigurarGrid()
        {
            grid.AutoGenerateColumns = false;
            grid.Columns.Clear();
            grid.Columns.AddRange(GetColunas());
        }

        public void SetDataSource(object list)
        {
            ConfigurarGrid();

            grid.DataSource = list;
        }

        public virtual DataGridViewColumn[] GetColunas()
        {
            throw new InvalidOperationException("É necessário sobreescrever o método GetColunas");
        }

        public TId GetId<TId>()
        {
            const int firstLine = 0, firstColumn = 0;

            object value = grid.SelectedRows[firstLine].Cells[firstColumn].Value;

            return (TId) Convert.ChangeType(value, typeof (TId));
        }
    }
}